/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.daos;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.models.ProductCategory;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TruongLQ
 */
public class CategoryDao {

    public Vector<ProductCategory> getAllProductCategories() {
        Vector<ProductCategory> categories = new Vector<>();

        final StringBuilder sql = new StringBuilder("select * from ProductCategory");
        try {
            ResultSet rs = CisConnection.getInstance().getSqlConnection()
                    .createStatement().executeQuery(sql.toString());

            while (rs.next()) {
                categories.add(new ProductCategory(rs.getInt(1), rs.getString(2)));
            }
            return categories;
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDao.class.getName()).log(Level.SEVERE, null, ex);
            return categories;
        }

    }
    
    public ProductCategory getProductCategoryByID(int id) {
        final StringBuilder sql = new StringBuilder("select * from ProductCategory where ProductCatID=?");
        try {
            PreparedStatement pst = CisConnection.getInstance().getSqlConnection()
                    .prepareStatement(sql.toString());
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();

            while (rs.next()) {
                return new ProductCategory(rs.getInt(1), rs.getString(2));
            }
            return null;
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDao.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }
}
